<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="卡密" prop="roleName">
                <el-input v-model="queryParams.keyNumber" placeholder="请输入卡密" clearable style="width: 180px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="是否激活" prop="isActivated">
                <el-select v-model="queryParams.isActivated" placeholder="请选择">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增卡密</el-button>
            </el-col>
        </el-row>
        <el-row :gutter="20">
            <!--用户数据-->
            <el-col :span="24" :xs="24">
                <el-table v-loading="loading" :data="keyList" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="100%" align="center" />
                    <el-table-column label="编号" align="center" key="id" prop="id" />
                    <el-table-column label="卡密卡号" align="center" key="keyNumber" prop="keyNumber" />
                    <el-table-column label="使用者" align="center" key="personName" prop="personName" />
                    <el-table-column label="激活时间" align="center" key="activation_time" prop="activationTime" />
                    <el-table-column label="到期时间" align="center" key="expiration_time" prop="expirationTime" />
                    <el-table-column label="是否激活" align="center" key="is_activated" prop="isActivated">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.isActivated == '0' ? 'success' : 'danger'">{{ scope.row.isActivated
                                == '0' ? '未激活' : '已激活' }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="是否封停" align="center" key="status" prop="status">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.status == '0' ? 'success' : 'danger'">{{ scope.row.status == '0'
                                ? '正常' : '封停' }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="卡密备注" align="center" key="remake" prop="isActivated" />
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-s-custom" @click="nobind(scope.row)">解绑</el-button>
                            <el-button size="mini" type="text" icon="el-icon-close" @click="handleUpdate(scope.row)">{{
                                scope.row.status == '0' ? '封停' : '解封' }}</el-button>
                            <el-button size="mini" type="text" icon="el-icon-delete"
                                @click="handleDelete(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize" @pagination="getList" />
            </el-col>
        </el-row>

        <el-dialog :title="新增卡密" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="卡密类型" prop="keyType">
                    <el-select v-model="form.keyTypes" placeholder="请选择卡密类型">
                        <el-option label="天卡" value="0"></el-option>
                        <el-option label="周卡" value="1"></el-option>
                        <el-option label="月卡" value="2"></el-option>
                        <el-option label="测试卡" value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="卡密备注" prop="remake">
                    <el-input v-model="form.remake" placeholder="请输入卡密备注" />
                </el-form-item>
                <el-form-item label="数量" prop="number">
                    <el-input v-model="form.number" placeholder="请输入卡密数量" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="open = false">取 消</el-button>
            </div>
        </el-dialog>
        <el-dialog title="提示" :visible.sync="centerDialogVisible" width="30%" center>
            <el-input type="textarea" v-model="keyList"></el-input>
            <span slot="footer" class="dialog-footer">
                <el-button @click="showKey()">取 消</el-button>
                <el-button type="primary" @click="showKey()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {
    listNz, addKey, delKey, shutDown,noBind

} from "@/api/system/nz";
export default {
    data() {
        return {
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                isActivated: undefined
            },
            // 显示搜索条件
            showSearch: true,
            // 弹出层
            open: false,
            centerDialogVisible: false,
            // 新增卡密表单数据
            form: {},
            // 卡密列表
            keyList: [],
            // 总条数
            total: 0,
            // 删除参数
            delParams: {
                id: undefined,
            },
            options: [
                {
                    value: '',
                    label: '全部'
                }, {
                    value: '0',
                    label: '未激活'
                }, {
                    value: '1',
                    label: '已激活'
                }],
        }
    },
    created() {
        this.getList();
    },
    methods: {
        // 获取卡密列表
        getList() {
            this.loading = true;
            listNz(this.queryParams).then(response => {
                this.keyList = response.rows;
                this.total = response.total;
                this.loading = false;
            }
            );
        },
        // 新增卡密弹窗
        handleAdd() {
            this.open = true;
        },
        // 删除卡密事件
        handleDelete(row) {
            this.delParams.id = row.id
            delKey(this.delParams).then(response => {
                this.$modal.msgSuccess("删除成功");
                this.getList();
            });
        },
        // 封停卡密
        handleUpdate(scope) {
            shutDown(scope.id).then(response => {
                this.$modal.msgSuccess(response.msg);
                this.getList();
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.$message({
                        message: '新增中,请耐心等待.',
                        type: 'success'
                    });
                    addKey(this.form).then(response => {
                        this.open = false;
                        this.centerDialogVisible = true;
                        this.keyList = response.data
                    });
                }
            });
        },
        // 回显卡密确定按钮
        showKey() {
            this.centerDialogVisible = false;
            this.getList();
        },

        // 搜索
        handleQuery() {
            this.loading = true;
            this.getList(this.queryParams).then(response => {
                this.keyList = response.rows;
                this.total = response.total;
                this.loading = false;
            }
            );
        },
        nobind(row){
            noBind(row.keyNumber).then(response => {
                this.$modal.msgSuccess(response.msg);
                this.getList();
            });
        },
    }
};
</script>